//
//  LoginViewController.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/9/24.
//  Copyright (c) 2015年 Tospur_hewei. All rights reserved.
//

#import "LoginViewController.h"
#import "BaseViewController.h"
#import <AFNetworking.h>
#import <MBProgressHUD.h>
#import "Encryption.h"
#import "NetWork.h"
#import <BMapKit.h>
#import "RegistViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *IdTextField;
@property (weak, nonatomic) IBOutlet UITextField *PassNum;
@property (nonatomic, ) NSInteger tag;
@property (nonatomic, ) NSInteger length;
@property (nonatomic, strong) NSString *signature;
@property (nonatomic ) NSInteger number;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSString *userID;
@property (nonatomic, strong) NSString *sessionid;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation LoginViewController
- (IBAction)LoginAction:(id)sender {
    
    if (self.IdTextField.text.length < 6 && self.PassNum.text.length < 6)
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号或密码格式错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
        
//        BaseViewController *baseVC = [[BaseViewController alloc]init];
//        [self presentViewController:baseVC animated:YES completion:nil];
    }
    else
    {
        _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _HUD.labelText=@"正在加载...";
        
        AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
        //申明返回的结果是json类型
        tx.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        tx.requestSerializer = [AFJSONRequestSerializer serializer];
        // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
        NSString *urlStr = @"http://172.20.5.142:7001/api/PusherLogin";
        self.signature = [[Encryption shareInstance]sha1];
        NSLog(@"+++++ %@",self.signature);
        NSString *passStr = [NSString stringWithFormat:@"%@%@",self.PassNum.text,@"tospur"];
        NSDictionary *dict = @{
                               @"appkey": @"tospur123",
                               @"signature": self.signature,
                               //@"sessionid": @"45785",
                               @"UserName": self.IdTextField.text,
                               @"Password": [[Encryption shareInstance]md5:passStr],
                               };
        [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"登录返回的数据 === %@",responseObject);
            self.userID = [responseObject objectForKey:@"userID"];
            self.userName = [responseObject objectForKey:@"userName"];
            self.sessionid = [responseObject objectForKey:@"sessionid"];
            self.status = [responseObject objectForKey:@"status"];
            
            [_HUD hide:YES afterDelay:1];
            
            //[self alertWarning];
            
            if ([self.status isEqualToString:@"200"])
            {
                _HUD.labelText=@"登录成功";
                BaseViewController *baseVC = [[BaseViewController alloc]init];
                [self presentViewController:baseVC animated:YES completion:nil];
                [[NSUserDefaults standardUserDefaults]setObject:self.IdTextField.text forKey:@"userName"];
                [[NSUserDefaults standardUserDefaults]setObject:self.PassNum.text forKey:@"passWord"];
                [[NSUserDefaults standardUserDefaults]setObject:@"autoLogin" forKey:@"loginFlag"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [NetWork shareInstance].sessionid = self.sessionid;
            }
//登录记录
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
            
            
        }];
        
    }

}

- (void)alertWarning
{
    if ([self.status isEqualToString:@"200"]) {
        _HUD.labelText=@"登陆成功";
    }
    if ([self.status isEqualToString:@"400"]) {
        _HUD.labelText=@"用户名不存在";
    }
    if ([self.status isEqualToString:@"500"]) {
        _HUD.labelText=@"密码错误";
    }
    else
    {
        _HUD.labelText=@"未知错误";
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
//键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.IdTextField resignFirstResponder];
    [self.PassNum resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)registAction:(id)sender {
    
    RegistViewController *registVC = [[RegistViewController alloc]init];
    [self presentViewController:registVC animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
